<?php
/**
 * @version 1.0 上传逻辑类
 * @internal
 * @link
 * @property
 * @see
 * @since
 * @uses
 *
 */
namespace app\user\logic;

use app\Api;
use app\user\logic\User as userLogicUser;
use app\model\UserOrder as ModelUserOrder;
use think\Db;

class Order extends Api
{
    private static $obj = null;

    public $order_state = [
        0
    ];

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 类入口
     * @return order|null
     */
    public static function getInstance()
    {
        if (is_null(self::$obj))
            self::$obj = new self();
        return self::$obj;
    }

    /**
     * 保率
     * @return float
     */
    public function protectRate()
    {
        return 0.005;
    }

    public function getElseOrderInfo($order_state, $express_state)
    {
        // 已完成
        if ($order_state == 200) {

        }

        // 已取消
        if ($order_state == 0) {

        }

        //待接单
        if ($order_state == 100 && $express_state == 100) {

        }

        //待取件
        if ($order_state == 100 && $express_state == 200) {

        }

        //待支付
        if ($order_state == 100 && $express_state == 300) {

        }
    }

    /**
     * 取消订单
     * @param $order_id
     * @param $message
     * @return bool
     */
    public function cancelOrder($order_id, $message)
    {
        try {
            ModelUserOrder::getInstance()->toUpdate(['order_id' => $order_id], ['order_state' => 0,'clear_reason' => $message,'clear_type'=>1]);
//            ModelUserOrder::getInstance()->toAdd(['order_id' => $order_id, 'message' => $message]);
            Db::commit(); //提交事务
            return true;
        } catch (\PDOException $e) {
            Db::rollback(); //回滚事务
            return false;
        }
    }

    /**
     * 获取订单列表条件
     * @param $str_type string 类型
     * @return array
     * 1：系统取消订单；0：客户已取消，5：订单异常；10，用户下单，待支付，未接单；20：未支付，已接单；25：未支付，已取件：30：已支付，已接单，已取件；40：已入库；50封箱；60：出库；65：中转；70：配送员接单；75：配送员已取件；80：配送完成；90：配送员取消
     */
    public function getOrderListWhere($str_type)
    {
        $where['is_delete'] = 0;
        switch ($str_type) {
            case 'completed' : // 已完成
                $where['order_state'] = ['in', [0,30,80]];
                break;
            case 'noCompleted' : //未完成
                $where['order_state'] = ['in', [10,20,25,40,50,60,65,70,75,90]];
                break;
            default :
                break;
        }
        return $where;
    }

    /**
     * 获取时间戳
     * @param $str_type string 类型
     * @param $str_time string 时间
     * @return bool|string
     */
    public function getTime($str_type, $str_time)
    {
        $arr_time = explode('-', $str_time);

        $start_time = empty($arr_time[0]) == false ? strtotime($arr_time[0]) : '';
        $end_time = empty($arr_time[1]) == false ? strtotime($arr_time[1]) : '';

        if ((!empty($end_time) && !empty($end_time) && $start_time > $end_time)) {
            return false;
        }

        $times = [];
        switch ($str_type) {
            case 'rightNow' :
                $times[0] = time() + 7200;
                break;
            case 'today' :
                $times[0] = $start_time;
                $times[1] = $end_time;
                break;
            case 'tomorrow' :
                $times[0] = $start_time + 86400;
                $times[1] = $end_time + 86400;
                break;
            case 'afterTomorrow' :
                $times[0] = $start_time + 172800;
                $times[1] = $end_time + 172800;
                break;
        }
        return implode('|', $times);
    }

    /**
     * 首重
     * @return int
     */
    public function getFirstHeavyFee()
    {
        return $first_heavy_fee = 0.01;
    }

    /**
     * 续重费
     * @return int
     */
    public function nextHeavyFee()
    {
        return $next_heavy_fee = 0.02;
    }

    /**
     * 续重
     * @return int
     */
    public function getNextHeavyFee($int_weight)
    {
        return $next_heavy_fee = ($int_weight - 1) * $this->nextHeavyFee();
    }

    /**
     * 生成唯一订单号
     * @return [type] [description]
     */
    static public function StrOrderOne()
    {
        /* 选择一个随机的方案 */
        mt_srand((double)microtime() * 1000000);

        return date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
    }

    /**
     * 验证收件人地址
     * @param $arr_address_info
     * @return bool
     */
    public function checkAreaInfo($arr_address_info)
    {

        $name = $arr_address_info['name']; //姓名
        if (empty($name)) return false;

        $phone = $arr_address_info['phone']; //手机号
        if (!preg_match('/^1[34578][0-9]{9}$/', $phone)) return false;

        $prov_id = $arr_address_info['provId'];
        $prov_name = userLogicUser::getInstance()->checkAreaId($prov_id); //验证省份id
        if ($prov_name != $arr_address_info['provName']) return false;

        //城市id;
        $city_id = $arr_address_info['cityId'];
        $city_name = userLogicUser::getInstance()->checkAreaId($city_id, $prov_id); //验证城市id
        if ($city_name != $arr_address_info['cityName']) return false;


        //县(区)id;
        $county_id = $arr_address_info['countyId'];
        $county_name = userLogicUser::getInstance()->checkAreaId($county_id, $city_id); //验证县区id
        if ($county_name != $arr_address_info['countyName']) return false;

        //街道id;
        $street_id = $arr_address_info['streetId'];
        $street_name = '';
        if ($street_id > 0) {
            $street_name = userLogicUser::getInstance()->checkAreaId($street_id, $county_id); //验证街道
            if ($street_name != $arr_address_info['streetName']) return false;
        }

        $detail_address = $arr_address_info['address']; //详细地址;
        if (empty($detail_address)) return false;

        return json_encode([
            'name' => $name,
            'phone' => $phone,
            'provId' => $prov_id,
            'provName' => $prov_name,
            'cityId' => $city_id,
            'cityName' => $city_name,
            'countyId' => $county_id,
            'countyName' => $county_name,
            'streetId' => $street_id,
            'streetName' => $street_name,
            'address' => $detail_address,
        ]);
    }
}
